package com.mobilebsc.app;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mobilebsc.R;
import com.mobilebsc.datamanager.IDataReadyListener;
import com.mobilebsc.datamanager.PerspectivesDataManager;
import com.mobilebsc.datatype.Perspective;

public class mobileBSCActivity extends Activity implements IDataReadyListener
{
    private LinearLayout            llMain;
    private int                     indexCount    = 1;
    private DataManager             dataManager   = new DataManager();
    private Intent                  intentPerspective;
    public static String            extra_message = "";
    private PerspectivesDataManager pdm           = new PerspectivesDataManager(this);

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_mbsc);

        llMain = (LinearLayout) findViewById(R.id.LinearLayout1);

        pdm.getAll();

        intentPerspective = new Intent(this, PerspectiveActivity.class);

        TextView tvBC = (TextView) findViewById(R.id.textViewBC);
        tvBC.setText("Home > BSC >");
    }

    private void showPerspectives()
    {
        for (Perspective p : dataManager.getPerspectives())
        {
            final int pId = p.getId();
            TextView tvPerspective = new TextView(this);

            Log.i("Test", Integer.toString(pId));

            tvPerspective.setText(p.getName());
            tvPerspective.setTextAppearance(this, R.style.mBSCPerspName);
            tvPerspective.setBackgroundColor(Color.BLUE);
            tvPerspective.setGravity(Gravity.CENTER_HORIZONTAL);
            tvPerspective.setClickable(true);
            tvPerspective.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View viewIn)
                {
                    intentPerspective.putExtra(extra_message,
                            Integer.toString(pId));
                    startActivity(intentPerspective);
                }
            });

            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            llp.setMargins(0, 3, 0, 0);
            tvPerspective.setLayoutParams(llp);

            indexCount++;

            llMain.addView(tvPerspective, indexCount);
        }
    }

    public void onClick_btn_Back(View view)
    {
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    public void dataReady()
    {
        List<Perspective> list = pdm.loadAll();

        dataManager.setPerspectives((ArrayList<Perspective>) list);

        showPerspectives();
    }
}
